package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.zktr.bean.BrandBean;
import com.zktr.pojo.Brand;
import com.zktr.pojo.BrandOne;
import com.zktr.pojo.PageBean;
import com.zktr.service.BrandService;

/**
 * @author yf  
 * @version 创建时间：2018年9月1日  下午5:31:28
 * tags
 */
@WebServlet("/BrandServlet")
public class BrandServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	BrandService bService=new BrandService();
	/**
	 * 查询所有品牌信息
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void quaryAllBrand(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		int pc = getPc(req);
		int ps = 10;
		PageBean<BrandBean> pageBean = bService.queryAllBrand(pc, ps);
		pageBean.setUrl(getUrl(req));
		req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		req.getSession().setAttribute("pb",pageBean);
		out.println("0");
	}
	private int getPc(HttpServletRequest req) {
		String value = req.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;
		}
		try{
			return Integer.parseInt(value);
		}catch(Exception e){
			return  1;
		}
	}
	/**
	 * 根据制定id,选择编辑品牌logo
	 * @param req
	 * @param res
	 * @throws IOException
	 * @throws ServletException 
	 * @throws SmartUploadException 
	 */
	public void updateBrand(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException{
		Brand brand = new Brand();
		PrintWriter out = res.getWriter();
		System.out.println("进入方法");
		String id = req.getParameter("id");
		SmartUpload smartUpload = new SmartUpload();
		ServletConfig config = this.getServletConfig();
		smartUpload.initialize(config, req, res);
		try {
			smartUpload.upload();
			com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
			String name = smartFile.getFilePathName();
			String path= req.getSession().getServletContext().getRealPath("/BrandServlet");
		    String replaceAll = path.replaceAll("BrandServlet", "img");
			String address=replaceAll +"\\"+ name;
			smartFile.saveAs(address);
			/*String endress=address.replaceAll("\\\\", "/");*/
			String relative="brand_img/"+name;
			System.out.println(relative);
			brand.setBrandlogo(relative);
			int row = bService.updateBrandlogo(brand, Integer.parseInt(id));
			System.out.println(row);
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		out.print("0");	
		
	}
	/**
	 * 根据制定id,存储两个单个信息
	 * @param req
	 * @param res
	 * @throws IOException
	 * @throws ServletException 
	 * @throws SmartUploadException 
	 */
	public void updateBrandOne(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException{
		BrandOne brand = new BrandOne();
		PrintWriter out = res.getWriter();
		SmartUpload smartUpload = new SmartUpload();
		ServletConfig config = this.getServletConfig();
		smartUpload.initialize(config, req, res);
		Date date = new Date();
	    long gmtMills = date.getTime() + 60000 * date.getTimezoneOffset();
		int x=(int) (gmtMills / 1000l);
		try {
			smartUpload.upload();
			com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
			String name = smartFile.getFilePathName();
			String path= req.getSession().getServletContext().getRealPath("/BrandServlet");
		    String replaceAll = path.replaceAll("BrandServlet", "img");
			String address=replaceAll +"\\"+ name;
			smartFile.saveAs(address);
			/*String endress=address.replaceAll("\\\\", "/");*/
			String relative="brand_img/"+name;
			brand.setBrandid(x);
			brand.setBrandlogo(relative);
			List<BrandOne> list=new  ArrayList<>();
			list.add(brand);
			req.getSession().setAttribute("brandone", list);
			brand.setBrandlogo(relative);
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		out.print("0");	
		
	}
	/**
	 * 根据制定id,选择编辑信息
	 * @param req
	 * @param res
	 * @throws IOException
	 * @throws ServletException 
	 * @throws SmartUploadException 
	 */
	public void updateBrands(HttpServletRequest req,HttpServletResponse res) throws IOException {
		Brand brand = new Brand();
		PrintWriter out = res.getWriter();
		System.out.println("进入方法");
		brand.setSortorder(Integer.parseInt(req.getParameter("sort")));
		brand.setBrandname(req.getParameter("name"));
		brand.setIsshow(Boolean.parseBoolean(req.getParameter("what")));
		System.out.println(req.getParameter("id"));
		brand.setBranddesc(req.getParameter("desc"));
		System.out.println(Integer.parseInt(req.getParameter("sort")));
int list=bService.updateBrand(brand,Integer.parseInt(req.getParameter("id")));
		if (list>0) {
			int pc = getPc(req);
			int ps = 15;
			PageBean<BrandBean> pageBean = bService.queryAllBrand(pc, ps);
			pageBean.setUrl(getUrl(req));
			req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
			req.getSession().setAttribute("pb",pageBean);
			out.print("0");
		}else {
			out.print("1");
		}
		
	}
	/**
	 * 根据制定id,更新品牌信息
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void quaryEditBrand(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		System.out.println(req.getParameter("id"));
		List<Brand> list=bService.queryAllBrandId(Integer.parseInt(req.getParameter("id")));
		if (list.size()>0) {
			req.getSession().setAttribute("Blist",list );
			out.print("0");
		}else {
			out.print("1");
		}
		
	}
	/**
	 * 根据制定id,更新品牌信息
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void quaryAddBrand(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
			out.print("0");
	}
	/**
	 * 根据制定id,添加
	 * @param req
	 * @param res
	 * @throws IOException
	 * @throws ServletException 
	 * @throws SmartUploadException 
	 */
	public void addBrands(HttpServletRequest req,HttpServletResponse res) throws IOException {
		Brand brand = new Brand();
		BrandOne bOne = new BrandOne();
		List<BrandOne> attribute = (List<BrandOne>) req.getSession().getAttribute("brandone");
		for (BrandOne brandOne : attribute) {
		   brand.setBrandid(brandOne.getBrandid());
		   brand.setBrandlogo(brandOne.getBrandlogo());
		}
		PrintWriter out = res.getWriter();
		brand.setSortorder(Integer.parseInt(req.getParameter("sort")));
		brand.setBrandname(req.getParameter("name"));
		brand.setIsshow(Boolean.parseBoolean(req.getParameter("what")));
		brand.setBranddesc(req.getParameter("desc"));
        int list=bService.addBrand(brand);
		if (list>0) {
			int pc = getPc(req);
			int ps = 15;
			PageBean<BrandBean> pageBean = bService.queryAllBrand(pc, ps);
			pageBean.setUrl(getUrl(req));
			req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
			req.getSession().setAttribute("pb",pageBean);
			out.print("0");
			System.out.println("添加成功");
		}else {
			out.print("1");
		}
		
	}
	
	/**
	 * 根据制定id,删除
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void deleteEditBrand(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		System.out.println(req.getParameter("id"));
		int list=bService.deleteOneBrandId(Integer.parseInt(req.getParameter("id")));
		System.out.println(list);
		if (list>0) {
			int pc = getPc(req);
			int ps = 10;
			PageBean<BrandBean> pageBean = bService.queryAllBrand(pc, ps);
			pageBean.setUrl(getUrl(req));
			req.getSession().setAttribute("pageBean",pageBean.getBeanList() );
			req.getSession().setAttribute("pb",pageBean);
			out.print("0");
			
		}else {
			out.print("1");
		}
		
	}
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();
		String servletPath = request.getServletPath();
		String queryString = request.getQueryString();
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		return contextPath + servletPath + "?" + queryString;
	}
	
	

}